package com.ruoyi.service.purchase.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.purchase.MesPurchaseReturn;
import com.ruoyi.domain.purchase.MesPurchaseReturnItem;
import com.ruoyi.mapper.purchase.MesPurchaseReturnItemMapper;
import com.ruoyi.mapper.purchase.MesPurchaseReturnMapper;
import com.ruoyi.service.purchase.IMesPurchaseReturnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 采购退货主表Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class MesPurchaseReturnServiceImpl implements IMesPurchaseReturnService {
    @Autowired
    private MesPurchaseReturnMapper mesPurchaseReturnMapper;

    @Autowired
    private MesPurchaseReturnItemMapper mesPurchaseReturnItemMapper;

    /**
     * 查询采购退货主表
     * 
     * @param returnId 采购退货主表主键
     * @return 采购退货主表
     */
    @Override
    public MesPurchaseReturn selectMesPurchaseReturnByReturnId(Long returnId) {
        MesPurchaseReturn returnOrder = mesPurchaseReturnMapper.selectMesPurchaseReturnByReturnId(returnId);
        if (returnOrder != null) {
            // 查询明细列表
            List<MesPurchaseReturnItem> itemList = mesPurchaseReturnItemMapper.selectItemListByReturnId(returnId);
            returnOrder.setReturnItemList(itemList);
        }
        return returnOrder;
    }

    /**
     * 查询采购退货主表列表（排除已完成）
     * 
     * @param mesPurchaseReturn 采购退货主表
     * @return 采购退货主表
     */
    @Override
    public List<MesPurchaseReturn> selectMesPurchaseReturnList(MesPurchaseReturn mesPurchaseReturn) {
        // 默认排除已完成的退货单
        if (StringUtils.isEmpty(mesPurchaseReturn.getStatus())) {
            mesPurchaseReturn.getParams().put("excludeStatus", "COMPLETED");
        }
        return mesPurchaseReturnMapper.selectMesPurchaseReturnList(mesPurchaseReturn);
    }

    /**
     * 新增采购退货主表
     * 
     * @param mesPurchaseReturn 采购退货主表
     * @return 结果
     */
    @Override
    @Transactional
    public int insertMesPurchaseReturn(MesPurchaseReturn mesPurchaseReturn) {
        mesPurchaseReturn.setCreateTime(DateUtils.getNowDate());
        mesPurchaseReturn.setCreateBy(SecurityUtils.getUsername());

        int rows = mesPurchaseReturnMapper.insertMesPurchaseReturn(mesPurchaseReturn);

        // 插入明细
        insertReturnItem(mesPurchaseReturn);

        return rows;
    }

    /**
     * 修改采购退货主表
     * 
     * @param mesPurchaseReturn 采购退货主表
     * @return 结果
     */
    @Override
    @Transactional
    public int updateMesPurchaseReturn(MesPurchaseReturn mesPurchaseReturn) {
        mesPurchaseReturn.setUpdateTime(DateUtils.getNowDate());
        mesPurchaseReturn.setUpdateBy(SecurityUtils.getUsername());

        // 删除原有明细
        mesPurchaseReturnItemMapper.deleteMesPurchaseReturnItemByReturnId(mesPurchaseReturn.getReturnId());

        // 插入新明细
        insertReturnItem(mesPurchaseReturn);

        return mesPurchaseReturnMapper.updateMesPurchaseReturn(mesPurchaseReturn);
    }

    /**
     * 批量删除采购退货主表
     * 
     * @param returnIds 需要删除的采购退货主表主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteMesPurchaseReturnByReturnIds(Long[] returnIds) {
        // 删除明细
        for (Long returnId : returnIds) {
            mesPurchaseReturnItemMapper.deleteMesPurchaseReturnItemByReturnId(returnId);
        }
        return mesPurchaseReturnMapper.deleteMesPurchaseReturnByReturnIds(returnIds);
    }

    /**
     * 删除采购退货主表信息
     * 
     * @param returnId 采购退货主表主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteMesPurchaseReturnByReturnId(Long returnId) {
        // 删除明细
        mesPurchaseReturnItemMapper.deleteMesPurchaseReturnItemByReturnId(returnId);
        return mesPurchaseReturnMapper.deleteMesPurchaseReturnByReturnId(returnId);
    }

    /**
     * 校验退货单号唯一性
     * 
     * @param returnCode 退货单号
     * @return 结果
     */
    @Override
    public String checkReturnCodeUnique(String returnCode) {
        MesPurchaseReturn returnOrder = mesPurchaseReturnMapper.selectMesPurchaseReturnByReturnCode(returnCode);
        if (StringUtils.isNotNull(returnOrder)) {
            return "1";
        }
        return "0";
    }

    /**
     * 执行采购退货
     * 
     * @param returnId 退货单ID
     * @return 结果
     */
    @Override
    @Transactional
    public int executeMesPurchaseReturn(Long returnId) {
        MesPurchaseReturn returnOrder = selectMesPurchaseReturnByReturnId(returnId);
        if (returnOrder == null) {
            throw new RuntimeException("退货单不存在");
        }

        // 更新退货单状态为已完成
        returnOrder.setStatus("COMPLETED");
        returnOrder.setOperatorId(SecurityUtils.getUserId());
        returnOrder.setOperatorName(SecurityUtils.getUsername());
        returnOrder.setUpdateTime(DateUtils.getNowDate());
        returnOrder.setUpdateBy(SecurityUtils.getUsername());

        return mesPurchaseReturnMapper.updateMesPurchaseReturn(returnOrder);
    }

    /**
     * 查询已完成的退货单列表（用于历史记录）
     * 
     * @param mesPurchaseReturn 采购退货主表
     * @return 采购退货主表集合
     */
    @Override
    public List<MesPurchaseReturn> selectCompletedReturnList(MesPurchaseReturn mesPurchaseReturn) {
        mesPurchaseReturn.setStatus("COMPLETED");
        return mesPurchaseReturnMapper.selectCompletedReturnList(mesPurchaseReturn);
    }

    /**
     * 新增退货明细信息
     * 
     * @param returnOrder 采购退货对象
     */
    public void insertReturnItem(MesPurchaseReturn returnOrder) {
        List<MesPurchaseReturnItem> returnItemList = returnOrder.getReturnItemList();
        Long returnId = returnOrder.getReturnId();
        if (StringUtils.isNotNull(returnItemList)) {
            List<MesPurchaseReturnItem> list = new ArrayList<MesPurchaseReturnItem>();
            for (MesPurchaseReturnItem returnItem : returnItemList) {
                returnItem.setReturnId(returnId);
                returnItem.setCreateTime(DateUtils.getNowDate());
                returnItem.setCreateBy(SecurityUtils.getUsername());
                list.add(returnItem);
            }
            if (list.size() > 0) {
                mesPurchaseReturnItemMapper.batchInsertMesPurchaseReturnItem(list);
            }
        }
    }

    /**
     * 审核采购退货单
     * 
     * @param returnId 退货单ID
     * @return 结果
     */
    @Override
    @Transactional
    public int approveMesPurchaseReturn(Long returnId) {
        MesPurchaseReturn returnOrder = mesPurchaseReturnMapper.selectMesPurchaseReturnByReturnId(returnId);
        if (returnOrder == null) {
            throw new RuntimeException("退货单不存在");
        }

        // 更新状态为已审核
        returnOrder.setStatus("APPROVED");
        returnOrder.setUpdateTime(DateUtils.getNowDate());
        returnOrder.setUpdateBy(SecurityUtils.getUsername());

        return mesPurchaseReturnMapper.updateMesPurchaseReturn(returnOrder);
    }

    /**
     * 根据原入库单ID查询退货单列表
     * 
     * @param receiptId 原入库单ID
     * @return 退货单列表
     */
    @Override
    public List<MesPurchaseReturn> selectReturnListByReceiptId(Long receiptId) {
        return mesPurchaseReturnMapper.selectReturnListByReceiptId(receiptId);
    }

    /**
     * 根据供应商ID查询退货单列表
     * 
     * @param supplierId 供应商ID
     * @return 退货单列表
     */
    @Override
    public List<MesPurchaseReturn> selectReturnListBySupplierId(Long supplierId) {
        return mesPurchaseReturnMapper.selectReturnListBySupplierId(supplierId);
    }

    /**
     * 根据仓库ID查询退货单列表
     * 
     * @param warehouseId 仓库ID
     * @return 退货单列表
     */
    @Override
    public List<MesPurchaseReturn> selectReturnListByWarehouseId(Long warehouseId) {
        return mesPurchaseReturnMapper.selectReturnListByWarehouseId(warehouseId);
    }

    /**
     * 根据退货类型查询退货单列表
     * 
     * @param returnType 退货类型
     * @return 退货单列表
     */
    @Override
    public List<MesPurchaseReturn> selectReturnListByType(String returnType) {
        return mesPurchaseReturnMapper.selectReturnListByType(returnType);
    }
}